package com.example.myapplication5;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class DonationActivity extends AppCompatActivity {

    private ViewPager vpBanner;
    private GridView gvCategory;
    private ListView lvProjects;

    private BannerAdapter bannerAdapter;
    private CategoryAdapter categoryAdapter;
    private ProjectAdapter projectAdapter;

    private List<Banner> bannerList = new ArrayList<>();
    private List<Category> categoryList = new ArrayList<>();
    private List<Project> projectList = new ArrayList<>();

    private int page = 1; // 分页加载页码
    private boolean isLoading = false; // 是否正在加载

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_donation);

        // 初始化控件
        vpBanner = findViewById(R.id.vp_banner);
        gvCategory = findViewById(R.id.gv_category);
        lvProjects = findViewById(R.id.lv_projects);

        // 初始化数据
        initBannerData();
        initCategoryData();
        initProjectData(page);

        // 设置适配器
        bannerAdapter = new BannerAdapter(this, bannerList);
        vpBanner.setAdapter(bannerAdapter);

        categoryAdapter = new CategoryAdapter(this, categoryList);
        gvCategory.setAdapter(categoryAdapter);

        projectAdapter = new ProjectAdapter(this, projectList);
        lvProjects.setAdapter(projectAdapter);

        // 轮播图自动播放
        startBannerLoop();

        // 列表上拉加载更多
        setListViewLoadMore();
    }

    // 初始化轮播图数据（用本地图片测试）
    private void initBannerData() {
        // 假设本地有三张图片，资源id为R.drawable.banner1、banner2、banner3
        // 注意：需要自己在res/drawable放图片，或用默认图标
        bannerList.add(new Banner(String.valueOf(R.mipmap.ic_launcher)));
        bannerList.add(new Banner(String.valueOf(R.mipmap.ic_launcher)));
        bannerList.add(new Banner(String.valueOf(R.mipmap.ic_launcher)));
    }

    // 初始化分类数据
    private void initCategoryData() {
        categoryList.add(new Category("教育助学", R.mipmap.ic_launcher));
        categoryList.add(new Category("医疗救助", R.mipmap.ic_launcher));
        categoryList.add(new Category("灾害救援", R.mipmap.ic_launcher));
        categoryList.add(new Category("环境保护", R.mipmap.ic_launcher));
        categoryList.add(new Category("扶贫济困", R.mipmap.ic_launcher));
        categoryList.add(new Category("动物保护", R.mipmap.ic_launcher));
        categoryList.add(new Category("文化保护", R.mipmap.ic_launcher));
        categoryList.add(new Category("其他公益", R.mipmap.ic_launcher));
    }

    // 初始化项目数据（模拟分页加载）
    private void initProjectData(int page) {
        // 每页加载5条
        for (int i = 0; i < 5; i++) {
            projectList.add(new Project(
                    "公益项目" + (page-1)*5 + i,
                    "这是第" + page + "页的项目描述...",
                    R.mipmap.ic_launcher
            ));
        }
    }

    // 轮播图自动播放
    private void startBannerLoop() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(() -> {
                    int current = vpBanner.getCurrentItem();
                    int next = current + 1;
                    if (next >= bannerList.size()) {
                        next = 0;
                    }
                    vpBanner.setCurrentItem(next);
                });
            }
        }, 3000, 3000); // 3秒切换一次
    }

    // 列表上拉加载更多
    private void setListViewLoadMore() {
        lvProjects.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // 当滚动到最后一项，并且不是正在加载时
                if (scrollState == SCROLL_STATE_IDLE
                        && lvProjects.getLastVisiblePosition() == projectList.size() - 1
                        && !isLoading) {
                    loadMoreData();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {}
        });
    }

    // 加载更多数据
    private void loadMoreData() {
        isLoading = true;
        Toast.makeText(this, "加载更多...", Toast.LENGTH_SHORT).show();

        // 模拟网络请求延迟
        new Handler().postDelayed(() -> {
            page++;
            initProjectData(page); // 加载下一页数据
            projectAdapter.notifyDataSetChanged(); // 刷新列表
            isLoading = false;
        }, 1000);
    }
}